package cn.itcast;

import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.core5.http.HttpEntity;
import org.apache.hc.core5.http.ParseException;
import org.apache.hc.core5.http.io.entity.EntityUtils;

import java.io.IOException;
import java.net.http.HttpClient;

public class CrawlerDemo1 {
    public static void main(String[] args) throws IOException, ParseException {
        //1.打开浏览器--->http客户端 httpClient
        CloseableHttpClient httpClient = HttpClients.createDefault();//创建一个默认的http客户端
        //2. 输入网址, 发起get请求--->httpGet
        HttpGet httpGet = new HttpGet("http://81.70.97.34:8081/index.html");
        //3. 回车发起请求
        CloseableHttpResponse response = httpClient.execute(httpGet);
        //4. 针对响应进行解析 获取到数据
        //判断状态码是否为200 200说明正常返回响应 拿到正常数据
        if(response.getCode() == 200) {
            //获取到响应体
            HttpEntity entity = response.getEntity();
            String content = EntityUtils.toString(entity, "utf-8");
            System.out.println(content);
        }

    }




}
